/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.main;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zxy.business.Ope_DB_Cent;
import com.zxy.common.Base64;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.EncryptUtil;
import com.zxy.tcp.ClientThread;
import com.zxy.tcp.CustTimeTask;
import com.zxy.tcp.MqttHandler;
import com.zxy.tcp.SelfTimeTask;
import com.zxy.tcp.ServerThread;
import com.zxy.tcp.ServerThreadHttp;

public class CenterDataMain
{
	public static void main(String[] args) throws Exception
	{
		System.out.println("CDIsys_v3 Main开始");		
//		float y = Com_Fun.ReadFloat(58901, 15981,false);        		
//		System.out.println("COD=>"+String.valueOf(y));		
		try
		{
			Init_Page.Init_Load();
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,CenterDataMain.class.toString());
		}		
		// 是否启用http协议接口
		if(Com_Para.bThread == true)
		{
			ServerThreadHttp athttp = new ServerThreadHttp("","");
			athttp.start();
		}
		// 中转节点Node客户端
		if(Com_Para.If_Child_Device == 1)
		{
			ClientThread st = new ClientThread();
			st.start();
		}
		// 中转节点Node服务端
		if(Com_Para.If_Parent_Device == 1)
		{
			if(Com_Para.AccSocketPort.indexOf(";") == -1)
			{
				ServerThread at = new ServerThread(Com_Para.AccSocketPort,0);
				at.start();
			}
			else{
				int i = 0;
				for(String strNum : Com_Para.AccSocketPort.split(";")){
					ServerThread at = new ServerThread(strNum,i);
					at.start();
					i ++;
				}
			}
		}
		// MQTT服务端
		if(Com_Para.MQTT_host.indexOf("tcp") == 0)
		{
			MqttHandler mh = new MqttHandler();
			mh.run();
		}

		/**** Session计时和删除数据 ***/
		Timer time2 = new Timer("CD_selftime");
		SelfTimeTask sthreadT = new SelfTimeTask();
		time2.scheduleAtFixedRate(sthreadT,10000,30000);

		// 自定义定时器
		int iIndex = 0;
		for(String num: Com_Para.CustTimeTaskNum.split(";"))
		{
			// 默认24小时
			int iLoop = 86400000;
			try
			{
				iLoop = Integer.valueOf(num);
			}
			catch(Exception es)
			{
			}
			Timer CustTime = new Timer("CD_CustTime_" + num);
			CustTimeTask ctt = new CustTimeTask();
			ctt.REFLECT_IN_CLASS = Com_Para.CustTimeREFLECT_IN_CLASS.split(";").length > iIndex ? Com_Para.CustTimeREFLECT_IN_CLASS.split(";")[iIndex] : "";
			CustTime.scheduleAtFixedRate(ctt,3000,iLoop);
			iIndex++;
		}
		System.out.println("CDIsys_v3 Main结束");
	}
}